﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingConsent
{
    public partial class HistoryConsent : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        public HistoryConsent(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            SetBandGridView();
            chaxun("", "");
        }

        #region GridView设置
        private void SetBandGridView()
        {
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 共用查询方法
        public void chaxun(string StatDate, string EndDate)
        {
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.WrittenConsentNursing;   //表单ID
            string sql = @"SELECT  *,Id,
                                   '' AS PICCshiyingzheng1, '' AS PICCshiyingzheng2, '' AS PICCshiyingzheng3, '' AS PICCshiyingzheng4, '' AS PICCshiyingzheng5,'' AS PICCshiyingzheng6,
                                   '' AS zhiguanguocheng1, '' AS zhiguanguocheng2, '' AS zhiguanguocheng3, '' AS zhiguanguocheng4,
                                   '' AS shiyongguocheng1, '' AS shiyongguocheng2, '' AS shiyongguocheng3, '' AS shiyongguocheng4, '' AS shiyongguocheng5,
                                   '' AS shiyongguocheng6, '' AS shiyongguocheng7, '' AS shiyongguocheng8,
                                   '' AS huanerjianhurenyijian, '' AS qianming, 
                                   '' AS qianmingshijian1, '' AS qianmingshijian2, '' AS qianmingshijian3, '' AS qianmingshijian4, '' AS qianmingshijian5,
                                   '' AS huanzheshouquanweituorenyijian, '' AS yuhuanzheguanxi, '' AS weituorenqianming, '' AS weituorenlianxifangshi,
                                   '' AS qianmingshijian21, '' AS qianmingshijian22, '' AS qianmingshijian23, '' AS qianmingshijian24, '' AS qianmingshijian25,
                                   '' AS huodeshouquandeyiwurenyuanqianming,
                                   '' AS qianmingshijian31, '' AS qianmingshijian32, '' AS qianmingshijian33, '' AS qianmingshijian34, '' AS qianmingshijian35,
                                   '' AS zhuzhiyishiqianming,
                                   '' AS qianmingshijian41, '' AS qianmingshijian42, '' AS qianmingshijian43, '' AS qianmingshijian44, '' AS qianmingshijian45
                                   FROM NursingFormRecord 
                                   WHERE FormId = {0}
                                   AND patientid = '{1}' 
                                   AND VisitId = {2} {3} 
                                   ORDER BY CreateTime DESC";
            string whereList = string.Empty;
            if (StatDate != "" && EndDate != "")
            {
                whereList = string.Format(" AND CONVERT(varchar(100),CreateTime, 23)>='{0}' AND  CONVERT(varchar(100),CreateTime, 23)<='{1}'", StatDate, EndDate);
            }
            sql = string.Format(sql, FormId, patientid, VisitId, whereList);
            DataTable dt = SqlHelper.ExecuteDataset(sql, CommandType.Text).Tables[0];
            DataTable dtt = dt.Clone();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    var NursingFormData = Convert.ToString(dr["NursingFormData"]);
                    WrittenConsent wc = new WrittenConsent();
                    wc = XmlUtil.XMLToObject<WrittenConsent>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dtt.NewRow();
                    #region
                    //PICC适应症
                    string PICCShiYingZheng = wc.PICCShiYingZheng;
                    string[] PICCArray;
                    if (!string.IsNullOrEmpty(PICCShiYingZheng))
                    {
                        PICCArray = PICCShiYingZheng.Split('#');
                        if (Convert.ToInt32(PICCArray[0]) == 1)
                        {
                            newRow["PICCshiyingzheng1"] = "√";
                        }
                        if (Convert.ToInt32(PICCArray[1]) == 1)
                        {
                            newRow["PICCshiyingzheng2"] = "√";
                        }
                        if (Convert.ToInt32(PICCArray[2]) == 1)
                        {
                            newRow["PICCshiyingzheng3"] = "√";
                        }
                        if (Convert.ToInt32(PICCArray[3]) == 1)
                        {
                            newRow["PICCshiyingzheng4"] = "√";
                        }
                        if (Convert.ToInt32(PICCArray[4]) == 1)
                        {
                            newRow["PICCshiyingzheng5"] = "√";
                        }
                    }
                    //PICC适应症文本框
                    newRow["PICCshiyingzheng6"] = wc.PICCShiYingZhengWenBenKuang;
                    //置管过程
                    string ZhiGuanGuoCheng = wc.ZhiGuanGuoCheng;
                    string[] ZhiGuanArray;
                    if (!string.IsNullOrEmpty(ZhiGuanGuoCheng))
                    {
                        ZhiGuanArray = ZhiGuanGuoCheng.Split('#');
                        if (Convert.ToInt32(ZhiGuanArray[0]) == 1)
                        {
                            newRow["zhiguanguocheng1"] = "√";
                        }
                        if (Convert.ToInt32(ZhiGuanArray[1]) == 1)
                        {
                            newRow["zhiguanguocheng2"] = "√";
                        }
                        if (Convert.ToInt32(ZhiGuanArray[2]) == 1)
                        {
                            newRow["zhiguanguocheng3"] = "√";
                        }
                        if (Convert.ToInt32(ZhiGuanArray[3]) == 1)
                        {
                            newRow["zhiguanguocheng4"] = "√";
                        }
                    }
                    //使用过程
                    string ShiYongGuoCheng = wc.ShiYongGuoCheng;
                    string[] ShiYongArray;
                    if (!string.IsNullOrEmpty(ShiYongGuoCheng))
                    {
                        ShiYongArray = ShiYongGuoCheng.Split('#');
                        if (Convert.ToInt32(ShiYongArray[0]) == 1)
                        {
                            newRow["shiyongguocheng1"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[1]) == 1)
                        {
                            newRow["shiyongguocheng2"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[2]) == 1)
                        {
                            newRow["shiyongguocheng3"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[3]) == 1)
                        {
                            newRow["shiyongguocheng4"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[4]) == 1)
                        {
                            newRow["shiyongguocheng5"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[5]) == 1)
                        {
                            newRow["shiyongguocheng6"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[6]) == 1)
                        {
                            newRow["shiyongguocheng7"] = "√";
                        }
                        if (Convert.ToInt32(ShiYongArray[7]) == 1)
                        {
                            newRow["shiyongguocheng8"] = "√";
                        }
                    }
                    //患儿监护人意见
                    newRow["huanerjianhurenyijian"] = wc.HuanErJianHuRenYiJian;
                    //签名
                    newRow["qianming"] = wc.QianMing;
                    //签名时间

                    //患者授权委托人意见
                    newRow["huanzheshouquanweituorenyijian"] = wc.HuanZheShouQuanWeiTuoRenYiJian;
                    //与患者关系
                    newRow["yuhuanzheguanxi"] = wc.YuHuanGuanXi;
                    //委托人签名
                    newRow["weituorenqianming"] = wc.WeiTuoRenQianMing;
                    //委托人联系方式
                    newRow["weituorenlianxifangshi"] = wc.WeiTuoRenLianXiFangShi;
                    //签名时间2

                    //获得授权的医务人员签名
                    newRow["huodeshouquandeyiwurenyuanqianming"] = wc.HuoDeShouQianDeYiWuRenYuanQiaMming;
                    //签名时间3

                    //主治医师签名
                    newRow["zhuzhiyishiqianming"] = wc.ZhuZhiYiShiQianMing;
                    //签名时间4

                    //ID隐藏，双击触发事件
                    newRow["Id"] = dr["ID"];
                    #endregion
                    dtt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dtt;
        }
        #endregion
        #region  查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string StatDate = this.StatDate.Text;
            string EndDate = this.EndDate.Text;
            chaxun(StatDate, EndDate);
        }
        #endregion

        #region 新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddConsent ac = new AddConsent(LoginUser, PatientInfo, "");
            ac.ShowDialog();
            if (ac.DialogResult == DialogResult.OK)
            {
                chaxun("", "");
            }
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            ConsentPrints frm = new ConsentPrints(LoginUser,PatientInfo,id);
            frm.Show();
        }
        #endregion

        #region 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除么？", "删除前确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dataStart = StatDate.Text;
                    string dateEnd = EndDate.Text;
                    chaxun(dataStart, dateEnd);
                }
            }
        }
        #endregion

        #region  整个大框
        private void HistoryConsent_Load(object sender, EventArgs e)
        {
            StatDate.DateTime = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;   //默认这个月的第一天
            EndDate.DateTime = DateTime.Now;
        }
        #endregion

        #region 开始时间
        private void StatDate_EditValueChanged(object sender, EventArgs e)
        {
            string StatDate = this.StatDate.Text;
            string EndDate = this.EndDate.Text;
            chaxun(StatDate, EndDate);
        }
        #endregion

        #region 结束时间
        private void EndDate_EditValueChanged(object sender, EventArgs e)
        {
            string StatDate = this.StatDate.Text;
            string EndDate = this.EndDate.Text;
            chaxun(StatDate, EndDate);
        }
        #endregion

        #region 双击事件显示值
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddConsent ac = new AddConsent(LoginUser, PatientInfo, id);
            ac.ShowDialog();
            if (ac.DialogResult == DialogResult.OK)
            {
                chaxun("", "");
            }
        }
        #endregion

    }
}
